package com.unytech.project.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.digester.Digester;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.xml.sax.SAXException;

import com.unytech.project.entity.system.Permission;


public class Xml2Permission {
	private List<Permission> permissionlist = new ArrayList<Permission>();
	
	public void addPerms(Permission per)
	{
		permissionlist.add(per);
	}
	public List<Permission> getPerms() {
		return permissionlist;
	}
	/**
	 * 解析XML文件，把XML文件转化为权限对象
	 * @param filename
	 * @param nodalpoint
	 * @return Xml2Permission
	 * @throws SAXException 
	 * @throws IOException 
	 * @throws Exception
	 */
	public Xml2Permission permParse(String filename, String nodalpoint) throws IOException, SAXException {
		Digester digester = new Digester();
		//不进行XML与相应的DTD的合法性验证
		digester.setValidating(false);  
		//将初始对象压入digester的stack 
		digester.push(new Xml2Permission());
		//指明匹配模式和要创建的类
		digester.addObjectCreate(nodalpoint, Permission.class );	
		
		//定义解析规则
		digester.addSetProperties(nodalpoint, "id", "permid");
		digester.addSetProperties(nodalpoint, "text", "permname");
//		digester.addSetProperties(nodalpoint, "type", "permtype");
		digester.addSetProperties(nodalpoint, "parentid", "parentid");
		digester.addSetProperties(nodalpoint, "uri", "permuri");
//		digester.addSetProperties(nodalpoint, "index", "permindex");
		
		//调用把Xml2Permission的addPerms方法把"tree/item"节点下的数据都进行解析保存
		digester.addSetNext(nodalpoint, "addPerms");
		
		//得到文件流
		InputStream input = this.getClass().getResourceAsStream(filename);
			
		//解析出对象
		Xml2Permission xmlper = (Xml2Permission)digester.parse(input);
		input.close();
		
		return xmlper;
	}
	
	/**
	 * 把权限XML文件解析为String
	 * return String
	 */		
	public String XML2String(String filename)
	{
		//把XML文件转换为字符串
		InputStream inputstream = this.getClass().getResourceAsStream(filename);
		SAXReader reader = new SAXReader();    
		Document document = null;
		try {
			document = reader.read(inputstream);
		} catch (DocumentException e) {
			System.out.println("把权限XML解析为字符串时出错！");
			System.out.println("出错类路径："+Xml2Permission.class);
			e.printStackTrace();
		} 
		String xmlString = document.asXML();
		System.out.println("----------把XML转换为String----------");
		System.out.println(xmlString);
		return xmlString;
	}
	

//	public static void main(String[] args) throws Exception{
//		Xml2Permission xml2per = new Xml2Permission();
//		xml2per = xml2per.permParse("/Permission.xml", "tree/item/item");
//		Permission per = null;
//		for (Iterator<Permission> it = xml2per.getPerms().iterator(); it.hasNext();)
//        {
//			per = (Permission)it.next();
//			System.out.println("id: "+per.getPermid());
//			System.out.println("perName: "+per.getPermName());
//			System.out.println("parentId: "+per.getParentId());
//			System.out.println();
//        }
		
//		xml2per.XML2String("/Permission.xml");
//	}
}
